<?php

/**
 * Class errorCode
 *
 * 1 - 2000 客户端显示返回错误
 * 2000 -   客户端自己处理错误
 */

class Common
{
    // pay
    public static $paySource = [
        'ALiPay' => 1,
        'WeChatPay' => 2,
        'ApplePay' => 3,
    ];

    public static $payFinish = 6000;

    public static $errorCode = [
        // 自定义错误
        'customError' => 1000,
        // 参数错误
        'paramsError' => 1001,
        // 注册错误
        'registerError' => 1002,
        // token错误
        'tokenError' => 1003,
        // 支付错误
        'payInfoError' => 1004,
        // 订单状态错误
        'orderInfo' => 1005,
        // 已经点赞过了
        'contentDigg' => 1006,
        // 系统错误
        'systemError' => 10000,
        // 数据库错误
        'dbError' => 10001,
    ];

    public static $status = [
        'extend_detail_mobileAuth' => [
            0 => 'noBind',
            1 => 'bind',
        ],
        'extend_detail' => [
            'source' => [
                'system' => 1,
                'weChat' => 2,
                'qq' => 3,
            ]
        ],
        'extend_mobile_category' => [
            'default_display' => [
                'true' => 1, // 手机端显示
                'false' => 0, // 手机端不显示
            ],
            'disabled' => [
                'on' => 0, // 启用
                'off' => 1, // 禁用
            ],
            'type' => [
                'news' => 1, // 资讯
                'video' => 2, // 视频
                'activity' => 3, // 活动
            ]
        ],
        'cmstop_content' => [
            'status' => [
                'published' => 6,	// 发布
            ]
        ],
        'extend_pay_order' => [
            'paySource' => [
                'weChat' => 2
            ],
            'paymentType' => [
                'onlinePay' => 1,
                'deliveryPay' => 2
            ],
            'status' => [
                'noPay' => 1,
                'pay' => 2,
                'finish' => 6
            ],
            'type' => [
                'activity' => 1,
            ],
            'isDelete' => [
                'delete' => 1,
                'noDelete' => 0,
            ]
        ],
        'extend_activity_sign' => [
            'type' => [
                'activity' => 1,
            ],
            'status' => [
                'pay' => 2, // 手机端显示
                'noPay' => 1, // 手机端不显示
                'finish' => 6,
            ],
        ],
    ];
}

// public function
function hideMobile($mobile)
{
    if (isMobile($mobile)) {
        return substr($mobile, 0, 3) . "****" . substr($mobile, 7, 4);
    }

    return $mobile;
}
function checkNotEmpty($params, $excludeKey = [], $checkKey = [])
{
    $newParams = [];
    if ($checkKey && is_array($checkKey)) {
        foreach ($checkKey as $key) {
            if (isset($params[$key])) {
                $newParams[$key] = $params[$key];
            }
        }
    }

    if ($newParams) {
        $params = $newParams;
    }

    if (is_array($params)) {
        foreach ($params as $k => $v) {
            if (in_array($k, $excludeKey, true)) {
                continue;
            }

            if (is_array($v)) {
                checkNotEmpty($v);
            }

            if (empty($v)) {
                return $k . ' 不允许为空';
            }
        }
    }
}

function isMobile($mobile)
{
    return preg_match('#^13[\d]{9}$|^14[4,5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,3,6,7,8]{1}\d{8}$|^18[\d]{9}$#', $mobile)
        ? true
        : false;
}

function userAvatar($userId)
{
    static $_userAvatar;
    if (!isset($_usernames[$userId]))
    {
        $_userAvatar[$userId] = loader::model('member/front', 'mapi')->get_photo($userId);
    }
    return $_userAvatar[$userId] . '?rdm=' . random(10);
}


function dd($info,$php_eol=false,$is_json = true){
    if($is_json){
        header('Content-Type:application/json; charset=utf-8');
        echo json_encode($info);
    }else{
        echo '<pre>';
        print_r($info);
    }

    if($php_eol === false){
        die;
    }else{
        echo $php_eol;
    }
}